﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChessTest.Chess.Items
{
    [Serializable]
    class CannonItem : LineStepItem
    {
        public override string DisplayName => "炮";


        public override bool CanOriginMove(Chessboard chessboard,int selectX, int selectY)
        {
            if (!base.CanOriginMove(chessboard,selectX, selectY)) return false;

            int obstacleCount = 0;

            for (int x = selectX; x < PositionX; x++)
                if (chessboard.GetChessman(x, PositionY) != null)
                    obstacleCount++;
            
            for (int x = PositionX+1; x <= selectX; x++)
                if (chessboard.GetChessman(x, PositionY) != null)
                    obstacleCount++;

            for (int y = selectY; y < PositionY; y++)
                if (chessboard.GetChessman(PositionX, y) != null)
                    obstacleCount++;
            for (int y = PositionY+1; y <= selectY; y++)
                if (chessboard.GetChessman(PositionX, y) != null)
                    obstacleCount++;

            ///中间没有任何东西
            if (obstacleCount == 0)
                return true;
            

            //中间有一个，但是落点不是旗子
            if(obstacleCount ==1)
            {
                return false;
            }

            if(obstacleCount ==2)
            {
                Chessman chessman = chessboard.GetChessman(selectX, selectY);
                if (chessman != null && chessman.IsBlack !=IsBlack)
                    return true;
            }

            return false;
        }

        public override List<Point> GetOriginCanMoveList(Chessboard chessboard)
        {
            List<Point> list = new List<Point>();
            bool hasChess = false;
            for(int x=PositionX+1;x< chessboard.MaxX;x++)
            {
                Chessman chessman = chessboard.GetChessman(x, PositionY);
                if (chessman != null)
                {
                    if (hasChess)
                    {
                        if(chessman.IsBlack !=IsBlack)
                            list.Add(new Point(x, PositionY));
                        break;
                    }
                    hasChess = true;
                }else if (!hasChess)
                    list.Add(new Point(x, PositionY));
            }

            hasChess = false;
            for (int x=PositionX-1;x>=0;x--)
            {
                Chessman chessman = chessboard.GetChessman(x, PositionY);
                if (chessman != null)
                {
                    if (hasChess)
                    {
                        if (chessman.IsBlack != IsBlack)
                            list.Add(new Point(x, PositionY));
                        break;
                    }
                    hasChess = true;
                }
                else if (!hasChess)
                    list.Add(new Point(x, PositionY));
            }

            hasChess = false;
            for(int y=PositionY+1;y< chessboard.MaxY;y++)
            {
                Chessman chessman = chessboard.GetChessman(PositionX, y);
                if (chessman != null)
                {
                    if (hasChess)
                    {
                        if (chessman.IsBlack != IsBlack)
                            list.Add(new Point(PositionX, y));
                        break;
                    }
                    hasChess = true;
                }
                else if (!hasChess)
                    list.Add(new Point(PositionX, y));
            }


            hasChess = false;
            for (int y = PositionY - 1; y >=0; y--)
            {
                Chessman chessman = chessboard.GetChessman(PositionX, y);
                if (chessman != null)
                {
                    if (hasChess)
                    {
                        if (chessman.IsBlack != IsBlack)
                            list.Add(new Point(PositionX, y));
                        break;
                    }
                    hasChess = true;
                }
                else if(!hasChess)
                    list.Add(new Point(PositionX, y));
            }
            return list;
        }

        public override int Score => 4;

    }
}
